package com.ly.springsolrtest.springsolr.service.impl;

import com.ly.springsolrtest.springsolr.bo.ArticleBo;
import com.ly.springsolrtest.springsolr.dao.ArticleBoSolrJpaDao;
import com.ly.springsolrtest.springsolr.service.ArticleBoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.solr.core.query.result.HighlightEntry;
import org.springframework.data.solr.core.query.result.HighlightPage;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Jpa数据操作方式
 *
 * @param
 * @author linweijian
 * @date 2018/3/30 12:50
 * @return
 */
@Service
public class ArticleBoJpaServiceImpl implements ArticleBoService {
    @Autowired
    private ArticleBoSolrJpaDao articleBoSolrJpaDao;

    @Override
    public void save(ArticleBo... articleBos) {
        for (ArticleBo articleBo : articleBos) {
            articleBoSolrJpaDao.save(articleBo);
        }
    }

    @Override
    public void deleteById(String id) {
        articleBoSolrJpaDao.deleteById(id);
    }

    @Override
    public ArticleBo getArticleBoById(String id) {
        return articleBoSolrJpaDao.getById(id);
    }

    @Override
    public List<ArticleBo> getHighlight(String keywords, int startIndex, int pageSize) {
        HighlightPage<ArticleBo> articleBoHighlightPage = articleBoSolrJpaDao.findArticleBoByKeywords(keywords, new PageRequest(startIndex, pageSize));
        // articleBoHighlightPage.getContent();获取内容
        for (HighlightEntry<ArticleBo> he : articleBoHighlightPage.getHighlighted()) {
            // A HighlightEntry belongs to an Entity (Book) and may have multiple highlighted fields (description)
            for (HighlightEntry.Highlight highlight : he.getHighlights()) {
                // Each highlight might have multiple occurrences within the description
                for (String snipplet : highlight.getSnipplets()) {
                    System.out.println(snipplet);
                }
            }
        }
        return null;
    }


}